import AlarmInfo from '../AlarmInfo/AlarmInfo';

export default class CongestionHandler {
    constructor() {
        this.alarmInfo = null;
        this.areaName = '';
        this.adcode = -1;
    }

    loadData(isInitChart, data) {
        let that = this;
        let areaName = data ? data.data.city : '';
        let adcode = data ? data.data.adcode : '';
        adcode = adcode[0] + adcode[1] + adcode[2] + adcode[3] + '00';
        if(/市辖区$/.test(areaName)) {
            areaName = data ? data.data.province : '';
            adcode = adcode[0] + adcode[1] + '0000';
        }
        if (areaName !== that.areaName) {
            minemap.util.getJSON('//203.81.245.38:48082/weather/alarmstatistic/' + adcode, function (error, data) {
                if (error) {
                    console.log(error);
                } else {
                    that.areaName = areaName;
                    that.adcode = adcode;

                    data = data ? data : {};
                    const alarmInfo = data.data.rows[0]
                    that.alarmInfo = {}
                    that.alarmInfo.total = alarmInfo['00'] ? (alarmInfo.total - alarmInfo['00']) : alarmInfo.total;
                    that.alarmInfo.alarm1 = alarmInfo['01'] ? alarmInfo['01'] : 0;
                    that.alarmInfo.alarm2 = alarmInfo['02'] ? alarmInfo['02'] : 0;
                    that.alarmInfo.alarm3 = alarmInfo['03'] ? alarmInfo['03'] : 0;
                    that.alarmInfo.alarm4 = alarmInfo['04'] ? alarmInfo['04'] : 0;            

                    if (isInitChart) {
                        that.updateChart();
                    }
                }
            });
        }
    }

    updateChart() {
        document.getElementById('alarm-chart').innerHTML = '';
        document.getElementById('alarm-container-name').innerHTML = this.areaName;
        this.chart = new AlarmInfo('alarm-chart', this.alarmInfo || {});
    }

    getInfoObj() {
        return this.infoObj;
    }
};